package de.lizenzverwaltung.model.dao.impl;

import java.lang.annotation.Inherited;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.lizenzverwaltung.model.dao.UserDAO;
import de.lizenzverwaltung.model.entities.Produktgruppe;
import de.lizenzverwaltung.model.entities.User;

public class UserDAOImpl extends DAOBase implements UserDAO {

	private Log logger = LogFactory.getLog(UserDAOImpl.class);

	/**
	 * {@inheritDoc}
	 */
	@Override
	public User getUser(String uid) {
		logger.debug("entering getUser");
		User user = null;
		
		String query = "SELECT * FROM user WHERE userid='"+uid+"'";
		try {
			Connection conn = openConnection();
			PreparedStatement pstmt = conn.prepareStatement(query);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				logger.debug("entering getUser - ResultSet");
				user = new User(rs.getString("userid"),rs.getString("passwort"),rs.getBoolean("admin"),rs.getBoolean("berechtigt"));
				user.setVorname(rs.getString("vorname"));
				user.setNachname(rs.getString("nachname"));
			}
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			logger.error("SQLException occured (Query=" + query + ")", e);
		}
		
		return user;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<User> getAllUsers() {
		logger.debug("entering getAllUsers");
		List<User> userListe = new ArrayList<User>();
		
		//
		String query = "SELECT * FROM user";
		try {
			Connection conn = openConnection();
			PreparedStatement pstmt = conn.prepareStatement(query);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()){
				logger.debug("entering getAllUsers - ResultSet");
				User user = new User(rs.getString("userid"),rs.getString("passwort"), rs.getBoolean("admin"), rs.getBoolean("berechtigt"));
				user.setVorname(rs.getString("vorname"));
				user.setNachname(rs.getString("nachname"));
				userListe.add(user);
			}
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			logger.error("SQLException occured (Query=" + query + ")", e);
		}
		return userListe;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean aendernBerechtigung(String uid, boolean berechtigung) {
		logger.debug("entering aendernBerechtigung");
		boolean result = false;
		
		//
		String query = "UPDATE user SET berechtigt =  ? WHERE  userid = ?";
		try {
			Connection conn = openConnection() ;
			PreparedStatement pstmt = conn.prepareStatement(query) ;
			pstmt.setBoolean(1, berechtigung);
			pstmt.setString(2, uid);
			pstmt.execute() ;
			pstmt.close();
			conn.close(); 
			result = true ;
		} catch (SQLException sqle) {
			logger.error("SQLException occured (Query=" + query + ")", sqle) ;
			result = false ;
		}
		return result;
	}

}
